package me.qi.kancha.controller;

import me.qi.kancha.controller.base.BaseController;
import me.qi.kancha.dto.core.DictionaryDTO;
import me.qi.kancha.service.DictionaryService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.vo.dictionary.DictionaryInsertVo;
import me.qi.kancha.vo.dictionary.DictionaryQueryVo;
import me.qi.kancha.vo.dictionary.DictionaryUpdateVo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典模块
 * 
* @author qi
* @date 2022-10-27 04:09:20
* @modified By：
*/
@RestController
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {

    private final DictionaryService service;

    public DictionaryController(DictionaryService service) {
        this.service = service;
    }

    /**
    * 字典模块-分页列表
    *
    * @param param
    * @return
    */
    @GetMapping
    public PageData<DictionaryDTO> listPaging(@Validated DictionaryQueryVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 listPaging 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.listPaging(param);
    }

    /**
    * 字典模块-新增
    *
    * @param param
    * @return
    */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public DictionaryDTO insert(@Validated @RequestBody DictionaryInsertVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 insert 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.insert(param);
    }

    /**
    * 字典模块-修改
    *
    * @param param
    * @return
    */
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public DictionaryDTO update(@Validated @RequestBody DictionaryUpdateVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 update 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.update(param);
    }

    /**
    * 字典模块-批量删除
    *
    * @param ids
    * @return
    */
    @PostMapping(value = "/deleteBatch", consumes = MediaType.APPLICATION_JSON_VALUE)
    public void deleteBatch(@RequestBody List<String> ids) {
        //如果有其他关联业务，建议重新定义删除方法
        // service.deleteBatch(ids);
        service.removeBatchByIds(ids);
    }

    /**
    * 字典模块-删除
    *
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id:"+ ID_REGX +"}")
    public void delete(@PathVariable String id) {
        //如果有其他关联业务-在 建议重新定义删除方法
        // service.delete(id);
        service.removeById(id);
    }

    /**
    * 字典模块-详情
    *
    * @return
    */
    @GetMapping(value = "/{id:"+ ID_REGX +"}")
    public DictionaryDTO detail(@PathVariable String id) {
        // 默认方法只根据id查询单表数据。
        // 如果接口需要返回关联表数据，建议调用自定义方法，在service通过多个单表查询拼接数据
        // service.detail(id);
        return service.getById(id);
    }
}




